"""
-*- coding:utf-8 -*-
最终版web自动化
Author: Free
DataTime: 2020/7/3 12:56

"""

import threading
import datetime
import pymysql as mysql
import configparser


class OperationMysql:
    def __init__(self):
        config = configparser.ConfigParser()
        config.read(r'D:\Git\这是币君自动化测试\ceshi\最终版web自动化\WebSite\config\config.ini')
        host = config.get('db', 'db_host')
        user = config.get('db', 'db_user')
        pwd = config.get('db', 'db_pass')
        name = config.get('db', 'db_name')
        port = eval(config.get('db', 'db_port'))
        # 打开数据库连接
        self.db = mysql.connect(host=host, user=user, port=port, password=pwd, database=name)

    def db_select(self, sql):
        try:
            cs1 = self.db.cursor()
            count = cs1.execute(sql)
            # print("查询到%d条数据:" % count)

            for i in range(count):
                result = cs1.fetchone()
                # print(result)

        except BaseException as e:
            print(e)

    def db_close(self):
        try:
            self.db.close()
        except BaseException as e:
            print(e)




def fun1():
    start = datetime.datetime.now()
    a = OperationMysql()
    sql = "SELECT type, availableVariation FROM trade_bill WHERE userId = 1309520 AND coinId = 69 AND type in (9,18,1,3,65,66,11,109,118,119);"

    a.db_select(sql)
    a.db_close()
    end = datetime.datetime.now()
    print("执行1个数据用时%d秒" % (end - start).seconds)


def fun2():
    start = datetime.datetime.now()
    a = OperationMysql()
    sql = "SELECT type, availableVariation FROM trade_bill WHERE userId = 1309520 AND coinId = 69 AND type in (9,18,1,3,65,66,11,109,118,119);"

    a.db_select(sql)
    a.db_close()
    end = datetime.datetime.now()
    print("执行1个数据用时%d秒" % (end - start).seconds)


def fun3():
    start = datetime.datetime.now()
    a = OperationMysql()
    sql = "SELECT type, availableVariation FROM trade_bill WHERE userId = 1309520 AND coinId = 69 AND type in (9,18,1,3,65,66,11,109,118,119);"

    a.db_select(sql)
    a.db_close()
    end = datetime.datetime.now()
    print("执行1个数据用时%d秒" % (end - start).seconds)


def fun4():
    start = datetime.datetime.now()
    a = OperationMysql()
    sql = "SELECT type, availableVariation FROM trade_bill WHERE userId = 1309520 AND coinId = 69 AND type in (9,18,1,3,65,66,11,109,118,119);"
    a.db_select(sql)
    a.db_close()
    end = datetime.datetime.now()
    print("执行1个数据用时%d秒" % (end - start).seconds)



def fun5():
    start = datetime.datetime.now()
    a = OperationMysql()
    sql = "SELECT type, availableVariation FROM trade_bill WHERE userId = 1309520 AND coinId = 69 AND type in (9,18,1,3,65,66,11,109,118,119);"

    a.db_select(sql)
    a.db_close()
    end = datetime.datetime.now()
    print("执行1个数据用时%d秒" % (end - start).seconds)


if __name__ == '__main__':
    start = datetime.datetime.now()
    thread_list = []
    thread_list.append(threading.Thread(target=fun1))
    thread_list.append(threading.Thread(target=fun2))
    thread_list.append(threading.Thread(target=fun3))
    thread_list.append(threading.Thread(target=fun4))
    thread_list.append(threading.Thread(target=fun5))

    for t in thread_list:
        t.start()
    end = datetime.datetime.now()
    print("总计用时%d秒" % (end - start).total_seconds())

